import QtQuick 2.0

// 图表曲线管理
Rectangle {

    id: root
    // public =====================================================================
    property real cFontsize: config.fontsize

    property var defaultColorList: [config.colorChannel1, config.colorChannel2, config.colorChannel3, config.colorChannel4]

    function init(){

        cCurveList   = {}
        cMinXList    = {}
        cMaxXList    = {}
        cMinYList    = {}
        cMaxYList    = {}
        cColorList   = {}
        cWidthList   = {}

        cIsFixedMinX = false
        cIsFixedMinY = false
        cIsFixedMaxX = false
        cIsFixedMaxY = false

        cFixedMinX = 1
        cFixedMinY = 0
        cFixedMaxX = 2
        cFixedMaxY = 1
    }


    function appendCurve(name, pointList, isBold, color){


        if(pointList.length === 0){
//            console.log("PointList "+name+" Size=0")
            return
        }
        cCurveList[name] = []


        let minx = pointList[0].x, maxx = pointList[0].x
        let miny = pointList[0].y, maxy = pointList[0].y
        for(let c = 0; c < pointList.length; c ++){
            cCurveList[name].push({
                                  "x": pointList[c].x,
                                  "y": pointList[c].y
                                  })
            minx = Math.min(pointList[c].x, minx)
            miny = Math.min(pointList[c].y, miny)
            maxx = Math.max(pointList[c].x, maxx)
            maxy = Math.max(pointList[c].y, maxy)
        }
        cMinXList[name] = minx
        cMinYList[name] = miny
        cMaxXList[name] = maxx
        cMaxYList[name] = maxy
        cColorList[name] = color
        cWidthList[name] = isBold ? 2 : 1
    }
    function setFixedRange(name, value){
        if(name === "minx"){
            cIsFixedMinX = true
            cFixedMinX = value
        }if(name === "maxx"){
            cIsFixedMaxX = true
            cFixedMaxX = value
        }if(name === "miny"){
            cIsFixedMinY = true
            cFixedMinY = value
        }if(name === "maxy"){
            cIsFixedMaxY = true
            cFixedMaxY = value
        }
    }
    function refresh(){         // 执行refresh后图表才会刷新
        chartview.refresh()
    }


    function getChartViewPosi(){
        return [chartview.x, chartview.y, chartview.width, chartview.height]
    }



    // private ====================================================================

    // 传递给ChartView的曲线信息  Key-曲线名 Value-曲线信息
    property var cCurveList:  ({})   // PointList
    property var cMinXList:   ({})   // 坐标轴范围
    property var cMaxXList:   ({})
    property var cMinYList:   ({})
    property var cMaxYList:   ({})
    property var cColorList:  ({})   // 颜色
    property var cWidthList:  ({})   // 线宽

    property bool cIsFixedMinX: false
    property bool cIsFixedMinY: false
    property bool cIsFixedMaxX: false
    property bool cIsFixedMaxY: false

    property real cFixedMinX: 0
    property real cFixedMinY: 0
    property real cFixedMaxX: 0
    property real cFixedMaxY: 0

    // 嵌的层数太多了…
    property real cMaxX: chartview.maxX
    property real cMaxY: chartview.maxY
    property real cMinX: chartview.minX
    property real cMinY: chartview.minY

    property string cAxisNameX: "循环数Cycles"
    property string cAxisNameY: " 荧光曲线(Ru)"

    property int  cDecimalX: 0       // 小数位数
    property int  cDecimalY: 0


    property int axisSizeL: chartview.axisYWidth     // (给坐标轴标题留的地方
    property int axisSizeB: chartview.axisXHeight
    property int legendHeight: 0


    anchors.fill: parent
//    clip: true

    // 坐标轴名称
    Text{       // 纵坐标Y(左)
        text: cAxisNameY
        rotation: -90
        anchors.centerIn: parent
        anchors.horizontalCenterOffset: -parent.width/2 + cFontsize
        anchors.verticalCenterOffset: -20
        font.pixelSize: cFontsize
    }
    Text{       // 横坐标X(底)
        text: cAxisNameX
        anchors.centerIn: parent
        anchors.verticalCenterOffset: parent.height/2 - cFontsize
        anchors.horizontalCenterOffset: 20
        font.pixelSize: cFontsize
    }

    CustomChartView{
        id: chartview
        x: axisSizeL; y: 0
        height: {
            return parent.height - axisSizeB
        }
        width: parent.width - axisSizeL

    }


    signal mousePositionChanged(var x, var y)
    signal mousePressed(var x, var y)
    signal mouseReleased(var x, var y)


    MouseArea{      // 图表交互
        anchors.fill: chartview
        hoverEnabled: true
        onPositionChanged: {
            let mousePos = mapToItem(this, mouseX, mouseY)
            var x = mousePos.x / this.width * (chartview.maxX-chartview.minX) + chartview.minX
            var y = (1-mousePos.y / this.height) * (chartview.maxY-chartview.minY) + chartview.minY
            root.mousePositionChanged(x, y)
        }
        onPressed: {
            let mousePos = mapToItem(this, mouseX, mouseY)
            var x = mousePos.x / this.width * (chartview.maxX-chartview.minX) + chartview.minX
            var y = (1-mousePos.y / this.height) * (chartview.maxY-chartview.minY) + chartview.minY
            root.mousePressed(x, y)
        }
        onReleased: {
            let mousePos = mapToItem(this, mouseX, mouseY)
            var x = mousePos.x / this.width * (chartview.maxX-chartview.minX) + chartview.minX
            var y = (1-mousePos.y / this.height) * (chartview.maxY-chartview.minY) + chartview.minY
            root.mouseReleased(x, y)
        }
    }

}
